# 创建交易和保存交易的方法是 transaction_create 和 save_transaction
# 查询交易的方法是 transaction_list 和 list_transactions
import datetime
from unittest.mock import patch

class TransactionService:
    def __init__(self):
        self.transactions = []

    def transaction_list(self):
        return self.transactions

    def transaction_create(self, payer, payee, amount, transaction_date, bill_number):
        transaction = {
            "payer": payer,
            "payee": payee,
            "amount": amount,
            "transaction_date": transaction_date,
            "bill_number": bill_number
        }
        self.transactions.append(transaction)
        return transaction

    def save_transaction(self, payer, payee, amount, transaction_date, bill_number):
        self.transaction_create(payer, payee, amount, transaction_date, bill_number)

    def list_transactions(self):
        return self.transaction_list()

def test_save_and_query_transactions():
    # 创建交易服务对象
    service = TransactionService()

    # 输入交易信息并保存
    transaction_1 = {
        "payer": "张三",
        "payee": "李四",
        "amount": 100.0,
        "transaction_date": datetime.datetime(2022, 1, 1, 0, 0),
        "bill_number": "20220101001"
    }
    service.save_transaction(**transaction_1)

    # 输入交易信息并保存
    transaction_2 = {
        "payer": "王五",
        "payee": "赵六",
        "amount": 200.0,
        "transaction_date": datetime.datetime(2022, 1, 2, 0, 0),
        "bill_number": "20220102001"
    }
    service.save_transaction(**transaction_2)

    # 输入交易信息并保存
    transaction_3 = {
        "payer": "钱七",
        "payee": "孙八",
        "amount": 300.0,
        "transaction_date": datetime.datetime(2022, 1, 3, 0, 0),
        "bill_number": "20220103001"
    }
    service.save_transaction(**transaction_3)

    # 查询交易信息并打印结果
    transactions = service.list_transactions()
    print(transactions)

# 覆盖 transaction_list 方法，模拟返回结果
@patch("TransactionService.transaction_list")
def test_list_transactions_with_return_value(mock_transaction_list):
    # 模拟返回结果
    transaction_1 = {
        "payer": "张三",
        "payee": "李四",
        "amount": 100.0,
        "transaction_date": datetime.datetime(2022, 1, 1, 0, 0),
        "bill_number": "20220101001"
    }
    mock_transaction_list.return_value = [transaction_1]

    # 查询交易信息并打印结果
    service = TransactionService()
    transactions = service.list_transactions()
    print(transactions)

# 覆盖 transaction_create 方法，模拟返回结果
@patch("TransactionService.transaction_create")
def test_save_transaction_with_return_value(mock_transaction_create):
    # 模拟返回结果
    transaction_1 = {
        "payer": "张三",
        "payee": "李四",
        "amount": 100.0,
        "transaction_date": datetime.datetime(2022, 1, 1, 0, 0),
        "bill_number": "20220101001"
    }
    mock_transaction_create.return_value = transaction_1

    # 输入交易信息并保存
    service = TransactionService()
    service.save_transaction(**transaction_1)

    # 输入交易信息并保存
    transaction_2 = {
        "payer": "王五",
        "payee": "赵六",
        "amount": 200.0,
        "transaction_date": datetime.datetime(2022, 1, 2, 0, 0),
        "bill_number": "20220102001"
    }
    mock_transaction_create.return_value = transaction_2
    service.save_transaction(**transaction_2)

    # 输入交易信息并保存
    transaction_3 = {
        "payer": "钱七",
        "payee": "孙八",
        "amount": 300.0,
        "transaction_date": datetime.datetime(2022, 1, 3, 0, 0),
        "bill_number": "20220103001"
    }
    mock_transaction_create.return_value = transaction_3
    service.save_transaction(**transaction_3)

if __name__ == "__main__":
    test_save_and_query_transactions()
    test_list_transactions_with_return_value()
    test_save_transaction_with_return_value()
